package com.lzj.friend;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.Iterator;

public class FriendReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        //声明一个变量就开始累加
        int friendCount = 0;
        //开始遍历推荐度
        Iterator<IntWritable> iterator = values.iterator();
        while (iterator.hasNext()) {
            IntWritable value = iterator.next();
            //判断是否为直接好友
            if (value.get() == -1) {
                return;
            }
            //开始进行累加
            friendCount += value.get();
        }
        //将好推荐度写出到HDFS
        context.write(key, new IntWritable(friendCount));
    }
}
